local blaze = require "blaze"
local game = require "club.threecard.sudoku"
local gamelib = require "club.threecard.gamelib"
local const = game.util.const
local M = {}

local HOME = "/club.threecard.sudoku/main/home"
local INGAME = "/club.threecard.sudoku/ingame"

local function until_home(route)
    return string.find(route.path, HOME)
end

local function until_versus_detail(route)
    return string.find(route.path, "/club.threecard.sudoku/versus%-detail")
end

function M.on_enter()
    local current = game.router.current()
    if string.find(current.path, "club.threecard.sudoku") then
        game.router.push(INGAME)
    else
        game.router.go(INGAME, function()
            game.router.stack():insert(HOME)
        end)
    end
end

function M.on_exit()
    local _, roomconfig = game.roomcache.get(gamelib.game_scene.room_config_id)
    if roomconfig.mode == "挑战" then
        game.router.back(until_versus_detail)
    else
        game.router.back(until_home)
    end
end

function M.on_exited()
    local intent = gamelib.game_scene.entrances.intent
    if intent == "继续火拼" then
        game.kbe.player().room_api
        :enter_by_config(const.LADDER_ROOM_ID)
        :next(function()
            gamelib.game_scene.entrances.intent = nil
            game.game_scene.enter(const.LADDER_ROOM_ID)
        end)
    elseif intent == "继续挑战" then
        print("[TODO]自动选择下一场开局")
        gamelib.game_scene.entrances.intent = nil
    end
end

return M